package com.ustadzmenjawab.crawler;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.ustadzmenjawab.shared.Article;

public class EramuslimCrawler extends CrawlerRoot {
	public static final String HOME_URL = "http://www.eramuslim.com/ustadz-menjawab";
	public static final String PATTERN = "ustadz-menjawab";
	public EramuslimCrawler() {
		super();
	}

	@Override
	public boolean isRelevantURL(String url) {
		if(url.contains("/ustadz-menjawab/")	&& 
				(url.endsWith(".htm") || url.contains("/arc/")))
			return true;
		return false;
		
	}

	@Override
	public boolean isRelevantPage(String content) {
		if(content.contains("<h3>Jawaban:</h3>"))
			return true;
		return false;
	}

	@Override
	public String getTitle(String body) {
		Document doc = Jsoup.parse(body);
		Element p = doc.select("h3").first();
		return p.text();
	}

	@Override
	public String getQuestion(String body) {
		int istart = body.indexOf("<div id=\"main-menu\">");
		int iend = body.indexOf("<h3>Jawaban:</h3>");
		body = body.substring(istart, iend);
		Document doc = Jsoup.parse(body);		
		String text = doc.text();
		return text;
	}

	@Override
	public String getArticle(String body) {
		int istart = body.indexOf("<div id=\"main-menu\">");
		int iend = body.indexOf("<div id=\"disqus_thread\">");
		body = body.substring(istart,iend);
		Document doc = Jsoup.parse(body);
		String p = doc.text();
		return p;
	}

	@Override
	public boolean insertContent(String url, String content) {
		try{
			Article article = new Article();
			String title = getTitle(content);
			if(isTitleExist(title, "eramuslim.com")){
				return false;
			}
			article.setJudul(title);
			article.setKategori("shariah");
			article.setSumber("eramuslim.com");
			article.setTahun("2011");
			article.setUrl(url);
			article.setUstadz("Sigit Pranowo, Lc");
			article.setArtikel(getArticle(content));
			article.setPertanyaan(getQuestion(content));
			db.insertArticle(article);
			return true;
		}catch(Exception e){
			System.out.println(" exception "+e.getClass() +" :" + e.getMessage());
			e.printStackTrace();
			insertFailure(url,"exception "+e.getClass() +" :" + e.getMessage());
			return false;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			int maxListPage = 5;
			EramuslimCrawler cr = new EramuslimCrawler();
			for(int i = 0; i <= maxListPage;i++){
				Document doc = Jsoup.connect(HOME_URL+"/arc/"+i+"/").timeout(0).get();
				Elements articlesLs = doc.select("a[href*="+PATTERN+"]");
				for(Element link:  articlesLs){
					try{
						String absHref = link.attr("abs:href");
						
						if(cr.isRelevantURL(absHref)){
							Document page = Jsoup.connect(absHref).timeout(0).get();
							String content = page.html();
							if(cr.isRelevantPage(content)){
								System.out.println(absHref +" is a relevant page");
								if(!cr.isUrlExist(absHref)){
									cr.insertContent(absHref, content);
								}
							}else{
								System.out.println(" is not a relevant page");
							}
						}
					}catch(Exception e){
						System.out.println(" exception "+e.getClass() +" :" + e.getMessage());
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
